const input = "777";

function* lexer(str) {
  let cursor = 0;
  let char = undefined;

  const number = () => {
    let value = "";
    for (; cursor <= str.length; cursor++) {
      char = str[cursor];
      if (char === "7") {
        // TODO 符合的语法组合起来
        value += char;
      } else {
        break;
      }
    }

    if (value.length >= 1) {
      return {
        type: "number",
        value,
      };
    }

    return null;
  };

  const eof = () => {
    char = str[cursor];
    if (char === undefined) {
      cursor++;
      return {
        type: "EOF",
      };
    }
    return null;
  };

  for (; cursor <= str.length; ) {
    const token = number() || eof();

    if (token) {
      yield token;
    } else {
      // 找到不符合的语法
      throw new SyntaxError(`Unexpected character "${char}" at ${cursor + 1}`);
    }
  }
}

console.log("start");
for (const token of lexer(input)) {
  console.log(token);
}
console.log("finish");
